<?php
namespace Home\Biz;

use Home\Biz\StringBiz;

class OrderInfoBiz{
	
	public static function searchorderinfolist($order_info_en){
		// 参数检查start
		if(empty($order_info_en['page_index'])){
			$order_info_en['page_index'] = 1;
		}
		
		if(empty($order_info_en['page_index'])){
			$order_info_en['page_size'] = C('PromoterConfig.listpage_search_default_size');
		}
		// 参数检查end
		
		// 搜索条件start
		$condition_array = array();
	
		$con = array();
		$order_id=$order_info_en['order_id'];
		if(!empty($order_id)){
			array_push($con, "o.order_id like '%$order_id%'");
		}
		$dealer_id=$order_info_en['dealer_id'];
		
		
		$order_type= StringBiz::getOrderTypeByDealerChannel($order_info_en['dealer_channel']);
		
		if(!empty($dealer_id)){
			array_push($con, "di.dealer_id like '%$dealer_id%'");
		}
		$dealer_name=$order_info_en['dealer_name'];
		if(!empty($dealer_name)){
			array_push($con, "di.dealer_name like '%$dealer_name%'");
		}
		
		$order_status = $order_info_en['order_status'];
		if(!empty($order_status)){
			array_push($con, "o.order_status = '$order_status'");
		}
		
		$payment_status = $order_info_en['payment_status'];
		if($payment_status != ''){
			array_push($con, "o.payment_status = '$payment_status'");
		}
		
		$start_date = $order_info_en['start_date'];
		$end_date = $order_info_en['end_date'];
		if(!empty($start_date)){
			if(!empty($end_date)){
				array_push($con, "o.order_date between '$start_date'"."and '$end_date' ");
			}
		}
		
		$dealer_type = $order_info_en['dealer_type'];
		if(!empty($dealer_type)){
			array_push($con, "di.dealer_type = '$dealer_type'");
		}
		
		// 搜索条件end
		$return_en = array();
		
		$condition_sql = implode(' AND ', $con);
		$user_id = cookie('login_user_id');
		
		$sale_en= M('sale_user')->where("sale_name='%s'",$user_id)->find();
		
		$no_businness="";
		if($sale_en!=null){
			//$no_businness=" and o.seller_alipay_no='$user_id'";
			
			$sale_name_join=M('sale_user')->field("GROUP_CONCAT(sale_name) as sale_name_join")->where(" FIND_IN_SET(sale_id, getSubSaleList('%s'))",$sale_en['sale_id'])->find();
			$sale_name_lst=$sale_name_join['sale_name_join'];
			$no_businness=" and FIND_IN_SET(o.seller_alipay_no,'$sale_name_lst')";
			
		}
		$countsql = "
			SELECT count(*) as num
			FROM order_info o
				INNER JOIN dealer_info di ON o.buyer_alipay_no = di.dealer_id 
				INNER JOIN order_payment AS p ON o.order_id = p. order_id  ";
		if(!empty($con)){
			$countsql = $countsql." WHERE ".$condition_sql." and o.order_type=".$order_type  .$no_businness." ORDER BY o.order_date DESC";
		}else{
			$countsql = $countsql." WHERE o.order_type=".$order_type .$no_businness." ORDER BY o.order_date DESC";
		}
		
		
		$sql = "
			SELECT  o.order_id, di.dealer_id,di.dealer_name,di.dealer_type,
				   o.order_status, o.order_type, o.payment_type,o.payment_method,
				   o.payment_status,o.goods_amount,o.discount_fee,o.order_amount,o.order_date,p.payment_date
			FROM order_info o
				INNER JOIN dealer_info di ON o.buyer_alipay_no = di.dealer_id 
				INNER JOIN order_payment AS p ON o.order_id = p. order_id ";
		$startRow =	(intval($order_info_en['page_index'])-1)*intval($order_info_en['page_size']);
		if(!empty($con)){
			$sql = $sql." WHERE ".$condition_sql." and o.order_type=".$order_type .$no_businness." ORDER BY o.order_date DESC limit ".$startRow.",".$order_info_en['page_size'];
		}else{
			$sql = $sql." WHERE o.order_type=".$order_type .$no_businness." ORDER BY o.order_date DESC  limit ".$startRow.",".$order_info_en['page_size'];
		}
 		/* echo $sql;
		return ; */
		
		$total_count_resule = M('')->query($countsql);
		$total_count = intval($total_count_resule[0]['num']);
		
		$return_en['total_count'] = $total_count;
		$lst_order_info_en = M('')->query($sql);
		
		
		foreach($lst_order_info_en as $key => $en){
			$lst_order_info_en[$key]['order_status_text'] = StringBiz::getPromoterOrderStatusText($en['order_status']);
			$lst_order_info_en[$key]['payment_status_text'] = StringBiz::getPaymentStatusText($en['payment_status']);
			$lst_order_info_en[$key]['payment_type_text'] = StringBiz::getPaymentTypeText($en['payment_type']);
			$lst_order_info_en[$key]['order_type_text'] = StringBiz::getOrderTypeText($en['order_type']);
			$lst_order_info_en[$key]['payment_method_text'] = StringBiz::getPaymentMethodText($en['payment_method']);
			$lst_order_info_en[$key]['dealer_type_text'] = StringBiz::getDealerTypeRevenueStatusText($en['dealer_type']);
		}
		
		$return_en['lst_order_info_en'] = $lst_order_info_en;
	
		$total_page = 0;
		$rem = $total_count % $order_info_en['page_size'];
		if($rem == 0){
			$total_page = $total_count / $order_info_en['page_size'];
		}else{
			$total_page = floor($total_count / $order_info_en['page_size']) + 1;
		}
		$return_en['total_page'] = $total_page;
		
		return $return_en;
	}

	
	/**
	 * Description: 获取可开具的经销商发票数据
	 * @version 2016年11月7日 下午3:47:14 
	 * @author lin.yujiao@whaley.cn
	 * @param unknown $order_info_en
	 */
	public static function searchPaperInvlist($order_info_en){
		// 参数检查START
		$page_index = empty($order_info_en['index'])? 1 : $order_info_en['index'];
		$page_size = empty($order_info_en['pageSize'])? C('PromoterConfig.listpage_search_default_size') : $order_info_en['pageSize'];
		// 参数检查END
	
		// 搜索条件START
		$con = self::paperInvListCondition($order_info_en);
		$condition_sql = implode(' AND ', $con);
		// 搜索条件END
		
		// 语句拼接 START
		$countsql = "
			SELECT count(*) as num
			FROM order_info o 
			INNER JOIN order_detail od on o.order_id = od.order_id
			LEFT JOIN order_consignment c on c.oid = od.oid
			INNER JOIN dealer_info di ON o.buyer_alipay_no = di.dealer_id " ." WHERE ".$condition_sql;
		
		$sql = "
			SELECT o.order_id, di.dealer_id, di.dealer_name, di.dealer_type,c.send_date,
				   o.order_status, o.order_type, o.payment_type, o.payment_method,
				   o.payment_status, o.order_date, o.invoice_kind,
				   od.oid, od.sku_name, od.qty, CASE WHEN  o.payment_type = 3 THEN od.account_amount WHEN o.payment_type = 4 THEN od.payment_amount END AS sale_amount,
				   di.finance_leader_cellphone, di.registered_address, di.tax_no, di.bank_no
			FROM order_info o
			INNER JOIN order_detail od on o.order_id = od.order_id
			LEFT JOIN order_consignment c on c.oid = od.oid
			INNER JOIN dealer_info di ON o.buyer_alipay_no = di.dealer_id ";
		$startRow =	(intval($page_index)-1)*intval($page_size);
		$sql = $sql." WHERE ".$condition_sql. " ORDER BY c.send_date ASC limit ".$startRow.",".$page_size;
		// 语句拼接 END
	
		$total_count_resule = M('')->query($countsql);
		$total_count = intval($total_count_resule[0]['num']);
	
		
		$lst_order_info_en = M('')->query($sql);
	
	
		foreach($lst_order_info_en as $key => $en){
			$lst_order_info_en[$key]['order_status_text'] = StringBiz::getPromoterOrderStatusText($en['order_status']);
			$lst_order_info_en[$key]['payment_status_text'] = StringBiz::getPaymentStatusText($en['payment_status']);
			$lst_order_info_en[$key]['payment_type_text'] = StringBiz::getPaymentTypeText($en['payment_type']);
			$lst_order_info_en[$key]['order_type_text'] = StringBiz::getOrderTypeText($en['order_type']);
			$lst_order_info_en[$key]['payment_method_text'] = StringBiz::getPaymentMethodText($en['payment_method']);
			$lst_order_info_en[$key]['dealer_type_text'] = StringBiz::getDealerTypeRevenueStatusText($en['dealer_type']);
		}
	
		
	
		$total_page = 0;
		$rem = $total_count % $page_size;
		if($rem == 0){
			$total_page = $total_count / $page_size;
		}else{
			$total_page = floor($total_count / $page_size) + 1;
		}
		
		$return_en = array();
		$return_en['total_count'] = $total_count;
		$return_en['lst_order_info_en'] = $lst_order_info_en;
		$return_en['total_page'] = $total_page;
		return $return_en;
	}
	
	
	/**
	 * Description: 构造纸质发票开具查询参数
	 * @version 2016年11月7日 下午4:17:57 
	 * @author lin.yujiao@whaley.cn
	 * @param unknown $order_info_en
	 */
	private static function paperInvListCondition($order_info_en){
		$con = array();
	
		$order_id = $order_info_en['order_id'];
		if(!empty($order_id)){
			array_push($con, "o.order_id like '%$order_id%'");
		}
	
		$dealer_id = $order_info_en['dealer_id'];
	
	
		if(!empty($dealer_id)){
			array_push($con, "di.dealer_id like '%$dealer_id%'");
		}
		$dealer_name = $order_info_en['dealer_name'];
		if(!empty($dealer_name)){
			array_push($con, "di.dealer_name like '%$dealer_name%'");
		}
	
		$order_status = $order_info_en['order_status'];
		if(!empty($order_status)){
			array_push($con, "o.order_status = '$order_status'");
		}
	
		$payment_status = $order_info_en['payment_status'];
		if($payment_status != ''){
			array_push($con, "o.payment_status = '$payment_status'");
		}
	
		$start_date = $order_info_en['start_date'];
		$end_date = $order_info_en['end_date'];
		if(!empty($start_date) && !empty($end_date)){
			array_push($con, "c.send_date between '$start_date'"."and '$end_date' ");
		}else if(!empty($start_date)){
			array_push($con, "c.send_date > '$start_date'");
		}else if(!empty($end_date)){
			array_push($con, "c.send_date < '$end_date'");
		}
	
		$start_o_date = $order_info_en['start_o_date'];
		$end_o_date = $order_info_en['end_o_date'];
		if(!empty($start_o_date) && !empty($end_o_date)){
			array_push($con, "o.order_date between '$start_o_date'"."and '$end_o_date' ");
		}else if(!empty($start_o_date)){
			array_push($con, "o.order_date > '$start_o_date'");
		}else if(!empty($end_o_date)){
			array_push($con, "o.order_date < '$end_o_date'");
		}
		
		$dealer_type = $order_info_en['dealer_type'];
		if(!empty($dealer_type)){
			array_push($con, "di.dealer_type = '$dealer_type'");
		}
		
	
	
		array_push($con, "o.order_type = 8 ");//经销商订单类型
		array_push($con, "od.ship_qty = od.qty ");
		array_push($con, "od.invoice_status = 1 ");//未开票  
		array_push($con, "((o.payment_type = 3 AND od.account_amount > 0) OR (o.payment_type = 4 AND od.payment_amount > 0))");
		return $con;
	}
	
	
	/**
	 * Description: 纸质发票预览数据构造 
	 * 1.新增同产品同价格合并功能
	 * 2.新增专/普票按照设定上限拆分预览功能
	 * @version 2016年11月14日 下午3:14:52 
	 * @author lin.yujiao@whaley.cn
	 * @param unknown $arr_oids
	 * @param unknown $msg
	 * @param unknown $oids
	 */
	public static function paperInvPreview($arr_oids, &$msg ,&$oids){
		$lst_vip_bag = self::getVipBagLst();
		$tmp_source_info = self::getPaperSourceInfo($arr_oids);
		$lst_result = array();
		if(sizeof($tmp_source_info) > 0){
			if(sizeof($tmp_source_info) != sizeof($arr_oids)){
				$msg = '检查出的有效数据与传入数据数量不符,具体原因请联系管理员';
			}
			//经销商映射与详情映射
			$invoice_mpg;
			$invoice_vip_mpg;
			$lst_source_info = self::buildPaperSourceInfo($tmp_source_info);
			//构造数据
			foreach($lst_source_info as $source_info){
				//若在映射中不存在则构造新的头部数据
				$tmp_invoice = $invoice_mpg[$source_info['key']];
				//构造明细
				$tmp_detail = self::buildPreviewDetail($source_info, $lst_vip_bag, $invoice_vip_mpg);
				if(empty($tmp_invoice)){
					$tmp_invoice = self::buildPreviewHead($source_info);
				}
				//累加头部金额
				$tmp_invoice['invoice_amount'] = round($tmp_invoice['invoice_amount'] + $tmp_detail['invoice_amount'],2);
				$tmp_invoice['sum_price'] = round($tmp_invoice['sum_price'] + $tmp_detail['sum_price'],2);
				$tmp_invoice['sum_tax'] = round($tmp_invoice['sum_tax'] + $tmp_detail['tax'],2);
				//添加明细与记录有效OID
				//$oids = array_combine($oids,explode(",",$source_info['oid']));
				$arr_oid = explode(",",$source_info['oid']);
				foreach ($arr_oid as $v_oid){
					array_push($oids,$v_oid);
				}
				//array_push($oids, $source_info['oid']);
				array_push($tmp_invoice['invoice_detail'], $tmp_detail);
				//添加头部数据映射
				$invoice_mpg[$source_info['key']] = $tmp_invoice;
			}
			//整理数据
			foreach($invoice_mpg as $mpg){
				//处理会员包
				$lst_vip_bag = $invoice_vip_mpg[$mpg['key']];
				if(!empty($lst_vip_bag)){
					foreach($lst_vip_bag as $vip_bag){
						//计算会员包总价内容
						$vip_bag['tax'] = round($vip_bag['sale_tax'] * $vip_bag['quantity'],2);
						$vip_bag['invoice_amount'] = round($vip_bag['amount'] * $vip_bag['quantity'],2);
						$vip_bag['sum_price'] = round($vip_bag['price'] * $vip_bag['quantity'],2);
						//添加会员包
						array_push($mpg['invoice_detail'], $vip_bag);
						//累加头部金额
						$mpg['invoice_amount'] = round($mpg['invoice_amount'] + $vip_bag['invoice_amount'],2);
						$mpg['sum_price'] = round($mpg['sum_price'] + $vip_bag['sum_price'],2);
						$mpg['sum_tax'] = round($mpg['sum_tax'] + $vip_bag['tax'],2);
					}
				}
				//追加进$lst_result
				array_push($lst_result, $mpg);
			}
			return $lst_result;
		}else{
			$msg = '数据无效,请检查';
		}
	}
	private static function buildPreviewDetail($source_info , $lst_vip_bag ,&$invoice_vip_mpg){
		/* $tmp_detail['item_name'] = $source_info['invoice_name']; 
		$tmp_detail['specification'] = $source_info['product_size'];*/
		$tmp_detail['item_name'] = $source_info['invoice_title'];
		$tmp_detail['specification'] = $source_info['invoice_name'];
		/* $tmp_detail['order_id'] = $source_info['order_id'];
		$tmp_detail['oid'] = $source_info['oid']; */
		$tmp_detail['lst_mpg'] = $source_info['lst_mpg'];
		$tmp_detail['quantity'] = $source_info['quantity'];
		//$tmp_detail['sale_amount_old'] = $source_info['amount'];
		$tmp_detail['tax_rate'] = $source_info['tax_rate'];
		$tmp_detail['unit'] = $source_info['unit'];
		$tmp_detail['referent_id'] = $source_info['referent_id'];
	
		$vip_bag = $lst_vip_bag[$tmp_detail['referent_id']];
		if(!empty($vip_bag)){//会员包存在
			//含税总价 = 订单明细.含税总价 - (会员包.含税总价 * 订单明细.数量)
			$tmp_detail['invoice_amount'] = round($source_info['invoice_amount'] - ($vip_bag['sale_amount'] * $tmp_detail['quantity']),2);
			//若映射不存在
			$tmp_vip_bag = $invoice_vip_mpg[$source_info['key']][$tmp_detail['referent_id']];
			if(empty($tmp_vip_bag)){
				$tmp_vip_bag['item_name'] = $vip_bag['invoice_title']; 
				//会员包无法对应到具体的订单
				//$tmp_vip_bag['order_id'] = $source_info['order_id'];
				$tmp_vip_bag['tax_rate'] = $vip_bag['tax'];
				$tmp_vip_bag['unit'] = $vip_bag['unit'];
				$tmp_vip_bag['quantity'] = $tmp_detail['quantity'];
				//$tmp_vip_bag['specification'] = $vip_bag['product_size'];
				$tmp_vip_bag['referent_id'] = $vip_bag['referent_id'];
				//含税单价与不含税单价不变
				$tmp_vip_bag['amount'] = $vip_bag['sale_amount'];
				$tmp_vip_bag['price'] = $vip_bag['sale_price'];
				$tmp_vip_bag['sale_tax'] = $vip_bag['sale_tax'];
				//税额,总价类根据数量进行刷新  预留到最后处理时
				//$tmp_vip_bag['invoice_amount'] = $vip_bag['referent_id'];
				//$tmp_vip_bag['sum_price'] = $vip_bag['referent_id'];
			}else{
				$tmp_vip_bag['quantity'] = ($tmp_vip_bag['quantity'] + $tmp_detail['quantity']);
			}
			$invoice_vip_mpg[$source_info['key']][$tmp_detail['referent_id']] = $tmp_vip_bag;
		}else{
			$tmp_detail['invoice_amount'] = $source_info['invoice_amount'];
		}
		//含税单价 = 含税总价/数量
		$tmp_detail['amount'] = round(($tmp_detail['invoice_amount'] / $tmp_detail['quantity']),6);
		//税额 = (含税总价 - (含税总价/(1+税率)))
		$tmp_detail['tax'] = round(( $tmp_detail['invoice_amount'] - ($tmp_detail['invoice_amount']/(1 + $tmp_detail['tax_rate'])) ),2);
		//不含税总价 = 含税总价 - 税额
		$tmp_detail['sum_price'] = round(($tmp_detail['invoice_amount'] - $tmp_detail['tax']),2);
		//不含税单价 = 不含税总价/数量
		$tmp_detail['price'] = round(($tmp_detail['sum_price'] / $tmp_detail['quantity']),6);
		return $tmp_detail;
	}
	/**
	 * Description: 构造预览头
	 * @version 2016年11月10日 下午7:57:20
	 * @author lin.yujiao@whaley.cn
	 * @param unknown $source_info
	 * @return unknown
	 */
	private static function buildPreviewHead($source_info){
		$tmp_invoice['key'] = $source_info['key'];
		$tmp_invoice['dealer_id'] = $source_info['dealer_id'];
		$tmp_invoice['order_id'] = $source_info['order_id'];
		$tmp_invoice['invoice_kind'] = $source_info['invoice_kind'];
		$tmp_invoice['invoice_name'] = $source_info['dealer_name'];
		$tmp_invoice['dealer_type'] = $source_info['dealer_type'];
		$tmp_invoice['finance_leader_cellphone'] = $source_info['finance_leader_cellphone'];
		$tmp_invoice['registered_address'] = $source_info['registered_address'];
		$tmp_invoice['tax_no'] = $source_info['tax_no'];
		$tmp_invoice['bank_no'] = $source_info['bank_no'];
		$tmp_invoice['receive_name'] = $source_info['receive_name'];
		$tmp_invoice['receive_tel'] = $source_info['receive_tel'];
		$tmp_invoice['receive_address'] = $source_info['receive_address'];
		$tmp_invoice['buyer_company_bank'] = ($source_info['bank_name'] . $source_info['bank_no']);
		$tmp_invoice['buyer_tax_no'] = $source_info['buyer_tax_no'];
		$tmp_invoice['buyer_company_contact'] = ($source_info['buyer_address'] . $source_info['buyer_cellphone']);
		$tmp_invoice['invoice_amount'] = 0;
		$tmp_invoice['sum_price'] = 0;
		$tmp_invoice['sum_tax'] = 0;
		//DKH:大客户
		$tmp_invoice['platform_code'] = 'DKH';
		//4:开票待回传
		$tmp_invoice['invoice_status'] = 4;
		//1:发票有效
		$tmp_invoice['status'] = 1;
		$tmp_invoice['invoice_detail'] = array();
		return $tmp_invoice;
	}
	/**
	 * Description: 获取构造前需要的原始数据
	 * @version 2016年11月10日 下午7:01:36
	 * @author lin.yujiao@whaley.cn
	 * @param unknown $arr_oids
	 */
	private static function getPaperSourceInfo($arr_oids){
		$con = array();
		array_push($con, "od.oid in ( '". implode("','", $arr_oids) . "' ) ");
		//	array_push($con, "oi.order_type = 8 ");
		//	array_push($con, "od.ship_qty = od.qty ");
		$condition_sql = implode(' AND ', $con);
		$sql = " SELECT ROUND((v.invoice_amount/v.quantity),2) AS key_price,v.* FROM (
				SELECT od.oid,od.qty as quantity,
				   p.invoice_name,p.tax as tax_rate,p.unit,p.product_size,p.referent_id,p.invoice_title,
				   CASE WHEN  oi.payment_type = 3 THEN od.account_amount WHEN oi.payment_type = 4 THEN od.payment_amount END AS invoice_amount,
				   oi.order_id,oi.invoice_kind,
				   di.dealer_id, di.dealer_name, di.dealer_type,
				   di.registered_tel as buyer_cellphone,
				   di.registered_address as buyer_address,
				   di.tax_no as buyer_tax_no, di.bank_no AS bank_no,
				   di.bank_name AS bank_name,
				   di.finance_leader as receive_name,
				   di.finance_leader_cellphone as receive_tel,
				   di.invoice_address as receive_address
				FROM whaley_trade.order_info oi
				INNER JOIN whaley_trade.order_detail od ON oi.order_id = od.order_id
				INNER JOIN whaley_product.product_split p ON od.sku_id =  p.sku_id
				INNER JOIN whaley_trade.dealer_info di ON oi.buyer_alipay_no = di.dealer_id ";
		//$sql = $sql." WHERE ".$condition_sql. " ORDER BY di.dealer_id, od.oid ";
		$sql = $sql." WHERE ".$condition_sql. " ) v ORDER BY v.dealer_id,v.invoice_amount ";
		$lst_source_info = M('')->query($sql);
		return $lst_source_info;
	}
	/**
	 * Description: 重新构造数据
	 * @version 2016年11月10日 下午7:01:36
	 * @author lin.yujiao@whaley.cn
	 * @param unknown $arr_oids
	 */
	private static function buildPaperSourceInfo(&$lst_source_info){
		//构造数据
		$amount_mpg = array();
		$new_lst;;
		foreach($lst_source_info as $source_info){
			//若在映射中不存在则构造新的头部数据
			$arr_amount = $amount_mpg[$source_info['dealer_id']];
			if(empty($arr_amount)){ //若无对应的价格则初始化
				$arr_amount = array(0);
			}
			//获取最后一个元素
			$invoice_amount = $arr_amount[count($arr_amount)-1];
			//根据发票类型来计算额度
	    	switch ($source_info['invoice_kind']){
	    		case 22://'普票' 额度 100W 即拆分上限为999999.999999 
	    			self::_buildSourceInfo(999999.999999, $invoice_amount, $source_info, $arr_amount, $new_lst);
	    			break;
	    		case 23://'专票' 额度 100W 即拆分上限为999999.999999 
	    			self::_buildSourceInfo(999999.999999, $invoice_amount, $source_info, $arr_amount, $new_lst);
	    			break;
	    		default:
	    	}
	    	$amount_mpg[$source_info['dealer_id']] = $arr_amount;
		}
		return $new_lst;
	}
	/**
	 * Description: 抽出对专票普票额度拆分的逻辑
	 * @version 2016年11月14日 下午7:42:13 
	 * @author lin.yujiao@whaley.cn
	 * $limit_amount : 限额数量
	 * $invoice_amount : $amount_mpg[$source_info['dealer_id']] => $arr_amount[last_index] =>$invoice_amount
	 * 某经销商拆分的发票金额后目前最后一项目目前的金额
	 * $source_info : 当前循环的发票数据
	 * $arr_amount : 某经销商拆分的金额数组
	 * $new_lst 拆分 重新构造后存放发票数据的集合
	 */
	private static function _buildSourceInfo($limit_amount, $invoice_amount, $source_info, &$arr_amount, &$new_lst){
		//若经销商金额记录数组中最后一条记录 + 当前记录的发票金额 大于限定金额 则进行拆分操作
		if (round($invoice_amount + $source_info['invoice_amount'] ,2) > $limit_amount) {
		    //获取单价
		    $price = round(($source_info['invoice_amount'] / $source_info['quantity']),6);
		    //获取原明细中商品的总数量
		    $max_qty = $source_info['quantity'];
		    //计算已用的空间 剩余金额能容纳几个产品  = (限定额度-已用额度)/单价
		    $last_num = intval(($limit_amount - $invoice_amount) / $price);
		    //可容纳数量大于0 则将可容纳数的金额以及数据追加
		    if($last_num > 0){
		    	//复制信息
		    	$last = $source_info;
		    	$last['quantity'] = $last_num;
		    	$last['invoice_amount'] = round($last_num * $price,2);
		    	//更新金额
		    	$arr_amount[count($arr_amount)-1] = round($invoice_amount + $last['invoice_amount'] ,2);
		    	$last['key'] = $last['dealer_id'] . '_' . count($arr_amount);
		    	//追加进新数据
		    	self::push_source_item($new_lst, $last);
		    	//原明细总数量减去本次使用的数量
		    	$max_qty = ($max_qty - $last_num);
		    }
		    //明细中现有最大数量已更新  存货空间已满 新起一个限额空间
		    //计算在极限额度之内可以容纳多少个该单价产品
		    $step = intval($limit_amount / $price);
		    if($max_qty > 0 && $step > 0){
			    do{
			    	$current = $source_info;
			    	//该明细数量为额度最大能容量数量
			    	$current_num = (($max_qty - $step) > 0) ? $step : $max_qty;
			    	$current['quantity'] = $current_num;
			    	$current['invoice_amount'] = round($current_num * $price,2);
			    	//追加金额
			    	array_push($arr_amount, $current['invoice_amount']);
			    	$current['key'] = $current['dealer_id'] . '_' . count($arr_amount);
			    	//追加进新数据
			    	self::push_source_item($new_lst, $current);
			    	//原明细总数量减去本次使用的数量
			    	$max_qty = ($max_qty - $current_num);
			    }while(($max_qty - $step) > 0);
		     }
    	}else{//未超过限定金额 将金额累加更新
		    $invoice_amount = round($invoice_amount + $source_info['invoice_amount'] ,2);
		    $arr_amount[count($arr_amount)-1] = $invoice_amount;
		    $current = $source_info;
		    $current['key'] = $current['dealer_id'] . '_' . count($arr_amount);
		    //追加进新数据
		    self::push_source_item($new_lst, $current);
    	}
	}
	/**
	 * Description: 合并追加行
	 * @version 2016年11月15日 下午4:51:41 
	 * @author lin.yujiao@whaley.cn
	 */
	private static function push_source_item(&$new_lst, &$item){
		$key = $item['key'] . '_' . $item['invoice_name'] . '_' . $item['key_price'];
		$old = $new_lst[$key];
		//若存在则累加
		if(!empty($old)){
			$old['quantity'] = ($old['quantity'] + $item['quantity']);
			$old['invoice_amount'] = ($old['invoice_amount'] + $item['invoice_amount']);
			array_push($old['lst_mpg'], array(
	    		'order_id' => $item['order_id'],
	    		'oid' => $item['oid']
	    	));
			$old['oid'] =  ($old['oid'] . ',' . $item['oid']);
			$old['order_id'] =  ($old['order_id'] . ',' . $item['order_id']);
			$new_lst[$key] = $old;
		}else{
			$item['lst_mpg'] = array(array(
	    		'order_id' => $item['order_id'],
	    		'oid' => $item['oid']
	    	));
			$new_lst[$key] = $item;
		}
	}
	/**
	 * Description: 获取VIP会员服务
	 * @version 2016年11月8日 下午2:51:44
	 * @author lin.yujiao@whaley.cn
	 */
	public static function getVipBagLst(){
		$lst_result;
		$lst_vip_bag = M ( 'product_split', '', 'DB_whaley_product' )->where("is_vip_bag = 1")->select();
		foreach($lst_vip_bag as $vip_bag){
			$lst_result[$vip_bag['id']] = $vip_bag;
		}
		return $lst_result;
	}
	
	
	/**
	 *
	 * Description: 保存纸质发票
	 * @version 2016年11月11日 下午2:40:05
	 * @author lin.yujiao@whaley.cn
	 */
	public static function paperInvSave($lst_invoice, $oids){
		$msg = self::paperInvValidate($oids);
		if(!empty($msg)){
			return $msg;
		}
		self::addPaperInv($lst_invoice);
	}
	
	private static function paperInvValidate($oids){
		$condition['oid'] = array('in',$oids);
		$condition['invoice_status'] = array('eq','1');
		$order_detail = M('order_detail', '', 'DB_whaley_trade')->where($condition)->select();
		if(sizeof($order_detail) != sizeof($oids)){
			return '当前订单明细可能已过期,请返回查询页面获取最新数据后重试';
		}
		return '';
	}
	private static function addPaperInv($lst_invoice){
		$login_user = cookie ( 'login_user_id' );
		if(empty($login_user)){
			$login_user = "whaleyinfo.order.orderBiz.paperInvSave";
		}
		foreach($lst_invoice as $invoice){
			$invoice['create_date'] = date("y-m-d H:i:s", time());
			$invoice['create_user'] = $login_user;
			$invoice['invoice_serial_no'] = CommonBiz::getInvoiceSeq();
			M( 'order_invoice' )->data( $invoice )->add();
			if(sizeof($invoice['invoice_detail']) > 0){
				foreach($invoice['invoice_detail'] as $detail){
					$detail['create_date'] = date("y-m-d H:i:s", time());
					$detail['create_user'] = $login_user;
					$detail['invoice_serial_no'] = $invoice['invoice_serial_no'];
					M ( 'order_invoice_item' )->data( $detail )->add();
					if(sizeof($detail['lst_mpg']) > 0){
						foreach($detail['lst_mpg'] as $order_invoice){
							$order_invoice['id'] = self::dealerOrderInvoiceId();
							$order_invoice['invoice_serial_no'] = $invoice['invoice_serial_no'];
							$order_invoice['create_date'] = date("y-m-d H:i:s", time());
							$order_invoice['create_user'] = $login_user;
							M ( 'dealer_order_invoice', '', 'DB_whaley_trade')->data( $order_invoice )->add();
							//查找更新相应订单明细表状态
							$order_detail = M('order_detail', '', 'DB_whaley_trade')->where(array('oid'=>$order_invoice['oid']))->find();
							if(!empty($order_detail)){
								//待开发票
								$order_detail['invoice_status'] = 4;
								$order_detail['create_date'] = date("y-m-d H:i:s", time());
								$order_detail['create_user'] = $login_user;
								M('order_detail')->where('oid="%s"', array($order_detail['oid']))->save($order_detail);
							}
						}
					}
					//更新相应订单明细表状态
			/* 		if(!empty($detail['oid'])){
						$order_detail = M('order_detail', '', 'DB_whaley_trade')->where(array('oid'=>$detail['oid']))->find();
						if(!empty($order_detail)){
							//待开发票
							$order_detail['invoice_status'] = 4;
							$order_detail['create_date'] = date("y-m-d H:i:s", time());
							$order_detail['create_user'] = $login_user;
							M('order_detail')->where('oid="%s"', array($order_detail['oid']))->save($order_detail);
						}
					} */
				}
			}
		}
	}
	/**
	 * Description:
	 * @version 生成发票映射表唯一编码
	 * @author lin.yujiao@whaley.cn
	 * @return string
	 */
	public static function dealerOrderInvoiceId(){
		$seq = CommonBiz::getNewSeq('member_id') + 10000000;
		return 'DOI' . $seq;
	}
}